<template>
	<div class="alarmConfiguration zy-content">
		<search :columns="searchData" @getSearch="getSearch(arguments)"></search>
		<div class="list-layout mt16">
			<div class="btns clear">
				<span class="title">{{ $t('common.listInformation') }}</span>
				<Button @click="createfaultCodeShowH(null, 1)">{{ $t('common.New') + '预警' }}</Button>
				<Button @click="createfaultCodeShowH(null, 2)">{{ $t('common.New') + '消息' }}</Button>
			</div>
			<!-- 表格 -->
			<div class="tableContent">
				<Table
					class="resizable-table"
					border
					@on-column-width-resize="onColumnWidthResize"
					:columns="columnsList"
					ellipsis
					ref="selection"
					stripe
					@on-selection-change="onSelectChange"
					:data="dataArr"
				>
					<template slot-scope="{ row }" slot="action">
						<span class="operationBtn" @click="createfaultCodeShowH(row, row.notificationMethod, 'check')"
							><i class="iconfont zy-common-chakan"></i>查看</span
						>
						<span class="operationBtn" @click="createfaultCodeShowH(row, row.notificationMethod, 'edit')"
							><i class="iconfont zy-common-bianji"></i>
							{{ $t('common.Edit') }}
						</span>
						<span class="operationBtn" @click="deleteOK(row)">
							<i class="iconfont zy-common-shanchu"></i>
							{{ $t('common.Delete') }}
						</span>
					</template>
				</Table>
			</div>
			<!-- 分页 -->
			<div class="pageWrap mt16">
				<pageLeftTitle :total="total" :selected="delIds.length" />
				<Page
					:total="total"
					:page-size="pageSize"
					:page-size-opts="pageSizeOption"
					:current="pageNum"
					@on-change="changePageNum"
					@on-page-size-change="changePageSize"
					show-sizer
					show-total
				/>
			</div>
		</div>
		<commonModal ref="commonModal"></commonModal>
		<addWarnModel ref="addWarnModel" @confirm="searchList"></addWarnModel>
		<addNoticeModel ref="addNoticeModel" @confirm="searchList"></addNoticeModel>
	</div>
</template>

<script>
import search from '@/components/curd/searchList.vue';
import fileDownload from 'js-file-download';
import toggleTableOperation from '@/mixins/toggleTableOperation';
import commonModal from '@/components/modalTypes/commonModal.vue';
import addNoticeModel from './modules/addNoticeModel';
import addWarnModel from './modules/addWarnModel';
import { getStatusList } from './modules/getStatusList.js';

export default {
	name: 'logPush',
	components: { search, commonModal, addNoticeModel, addWarnModel },
	mixins: [toggleTableOperation],
	data() {
		return {
			loading: false,
			create: false,
			searchParams: {},
			pageNum: 1,
			pageSize: 10,
			total: 0,
			searchData: [
				{
					title: '模板名称',
					key: 'templateName',
				},
				{
					title: '模板类型',
					key: 'notificationMethod',
					type: 'select',
					dicData: [
						{
							label: '消息',
							value: 2,
						},
						{
							label: '预警',
							value: 1,
						},
					],
				},
				{
					title: '模版内容',
					key: 'templateContent',
				},
			],
			columns: [
				{
					title: '序号',
					key: 'index',
					width: 80,
					align: 'center',
					render: (h, params) => {
						return h('div', params.row._index + (this.pageNum - 1) * this.pageSize + 1);
					},
				},
				{
					title: '模版名称',
					key: 'templateName',
					minWidth: 130,
					align: 'center',
					resizable: true,
					tooltip: true,
				},

				{
					title: '模版类型',
					key: 'notificationMethod',
					minWidth: 130,
					align: 'center',
					resizable: true,
					tooltip: true,
					render: (h, params) => {
						return h('div', getStatusList().find((item) => item.value == params.row.templateType).label);
					},
				},
				{
					title: '所属工厂',
					key: 'functionalLocationName',
					minWidth: 130,
					align: 'center',
					resizable: true,
					tooltip: true,
				},
				{
					title: '通知类型',
					key: 'notificationMethod',
					minWidth: 150,
					align: 'center',
					resizable: true,
					render: (h, params) => {
						return h('div', params.row.notificationMethod == 1 ? '预警' : '消息');
					},
				},
				{
					title: '计划',
					key: 'planName',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '提醒时间',
					key: 'reminderTime',
					width: 130,
					align: 'center',
					resizable: true,
					tooltip: true,
				},
				{
					title: '模版内容',
					key: 'templateContent',
					minWidth: 150,
					align: 'center',
					resizable: true,
					tooltip: true,
				},
				{
					title: '创建人',
					key: 'createName',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},
				{
					title: '创建时间',
					key: 'createTime',
					minWidth: 150,
					align: 'center',
					resizable: true,
				},

				{
					title: `${this.$t('common.operation')}`,
					slot: 'action',
					fixed: 'right',
					minWidth: 260,
					align: 'center',
				},
			],
			dataArr: [],
			pageSizeOption: [10, 20, 30, 40],
			delIds: [],
			selection: [],
		};
	},
	mounted() {
		this.getTableColumnsWidth(this.columns);
		this.searchList();
	},
	methods: {
		getSearch(arg) {
			this.searchParams = JSON.parse(JSON.stringify(arg[0]));
			const { time, time2 } = this.searchParams;
			if (time && time.length == 2) {
				this.searchParams.creatStartTime = time[0];
				this.searchParams.creatEndTime = time[1];
				delete this.searchParams.time;
			}
			if (time2 && time2.length == 2) {
				this.searchParams.updateStartTime = time2[0];
				this.searchParams.updateEndTime = time2[1];
				delete this.searchParams.time2;
			}
			this.pageNum = 1;
			this.searchList();
		},
		createfaultCodeShowH(row, addType, showType) {
			if (addType == 1) {
				this.$refs.addWarnModel.show(row, showType);
			} else this.$refs.addNoticeModel.show(row, showType);
		},
		deleteOK(row) {
			this.$refs.commonModal.showModal({
				title: '删除提醒',
				type: 'warn',
				desc: '是否确认删除？',
				submit: () => {
					this.axios({
						url: '/dms/messageWarning/deleteById',
						method: 'post',
						data: [row.id],
					}).then((res) => {
						if (res.code === 200) {
							this.$Message.success({
								content: res.msg,
								duration: 6,
								closable: true,
							});
						} else {
							this.$Message.warning({
								content: res.msg,
								duration: 6,
								closable: true,
							});
						}
						this.searchList();
					});
				},
			});
		},
		reset() {
			this.searchParams = {};
			this.changeNum();
		},
		//分页
		changeNum() {
			this.pageNum = 1;
			this.searchList();
		},
		changePageSize(pageSize) {
			this.pageSize = pageSize;
			this.searchList();
		},
		changePageNum(val) {
			this.pageNum = val;
			this.searchList();
		},
		//查询列表
		searchList() {
			let params = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				...this.searchParams,
			};
			this.axios({
				method: 'post',
				url: '/dms/messageWarning/queryMessageWarningPlanList',
				data: params,
			}).then((res) => {
				if (res.code === 200) {
					this.dataArr = res.data.records;
					this.total = res.data.total;
				} else {
					this.$Message.warning({
						content: res.msg,
						duration: 6,
						closable: true,
					});
				}
			});
		},
		//导出
		exportTemplateClick() {
			let params = {
				...this.searchParams,
			};
			if (params.creatStartTime == null || params.creatStartTime == null) {
				this.$Message.warning('请选择创建时间或者修改时间');
				return;
			}
			const filName = '推送日志记录';
			this.axios({
				method: 'post',
				url: '/dms/thirdPartyLog/export',
				data: params,
			}).then((res) => {
				if (res.code == 555) {
					this.$Message.warning(res.msg);
				} else {
					this.axios({
						method: 'post',
						url: '/dms/thirdPartyLog/export',
						responseType: 'blob',
						data: params,
					}).then((res) => {
						fileDownload(res, filName + '.xlsx');
					});
				}
			});
		},
	},
};
</script>